package com.zhang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhang.entity.ShoppingCategory1;
import com.zhang.mapper.ShoppingCategory1Mapper;
import com.zhang.service.ShoppingCategory1Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ZhangD
 * @since 2021-07-13
 */
@Service
public class ShoppingCategory1ServiceImpl extends ServiceImpl<ShoppingCategory1Mapper, ShoppingCategory1> implements ShoppingCategory1Service {

    @Autowired
    private ShoppingCategory1Mapper shoppingCategory1Mapper;


    @Override
    public List<ShoppingCategory1> selectShoppingCategory1(){
        QueryWrapper<ShoppingCategory1> wrapper = new QueryWrapper<>();
        wrapper.isNotNull("id");
        return shoppingCategory1Mapper.selectList(wrapper);
    }

    @Override
    public Page<ShoppingCategory1> category1Name(String name, int pageNo, int pageSize) {
        Page<ShoppingCategory1> page = new Page<>(pageNo,pageSize);
        QueryWrapper<ShoppingCategory1> wrapper = new QueryWrapper<>();
        wrapper.like("category1_name",name);
        return shoppingCategory1Mapper.selectPage(page,wrapper);
    }

    @Override
    public int changeValue(String id) {
        ShoppingCategory1 category1 = new ShoppingCategory1();
        UpdateWrapper<ShoppingCategory1> wrapper = new UpdateWrapper<>();
        wrapper.eq("id",id);
        if(shoppingCategory1Mapper.selectById(id).getValue()) {
            category1.setValue(false);
        }else {
            category1.setValue(true);
        }
        return shoppingCategory1Mapper.update(category1,wrapper);

    }

    @Override
    public ShoppingCategory1 getCategory(String id) {
        return shoppingCategory1Mapper.selectById(id);
    }

    @Override
    public int addCategory1(String name, String record) {
        ShoppingCategory1 category1 = new ShoppingCategory1();
        category1.setCategory1Name(name);
        category1.setCategory1Record(record);
        return shoppingCategory1Mapper.insert(category1);

    }

    @Override
    public int updateCategory1(String id, String category1Name, String category1Record) {
        UpdateWrapper<ShoppingCategory1> wrapper = new UpdateWrapper<>();
        wrapper.eq("id",id);
        ShoppingCategory1 category1 = new ShoppingCategory1();
        if (category1Record!=null){
            category1.setCategory1Record(category1Record);
        }
        if (category1Name!=null) {
            category1.setCategory1Name(category1Name);
        }
        return shoppingCategory1Mapper.update(category1,wrapper);
    }


}
